package com.koron.css2.contractmanage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.mapper.ContractManageMapper;
import com.koron.css2.contractmanage.querybean.ContractManageQueryBean;
import com.koron.css2.contractmanage.vo.ContractManageVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

public class NewContractManageList implements ServerInterface {
    /**
     * 合同状态  用于数据字典取值
     */
    private static final String CONTRACT_STATUS = "CONTRACT_STATUS";

    /**
     * 合同类型  用于数据字典取值
     */
    private static final String CONTRACT_TYPE = "CONTRACT_TYPE";

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ContractManageQueryBean queryBean = JsonUtils.objectToPojoIgnoreNone(req.getData(), ContractManageQueryBean.class);
            Map<String, String> contractStatusMap = Tools.mapDicByCode(factory, CONTRACT_STATUS);
            Map<String, String> contractTypeMap = Tools.mapDicByCode(factory, CONTRACT_TYPE);
            ContractManageMapper mapper = factory.getMapper(ContractManageMapper.class);

            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            List<ContractManageVO> contractManageList = mapper.selectContractManageList(queryBean);
            for (ContractManageVO contractManageVO : contractManageList) {
                if (contractStatusMap != null && contractStatusMap.containsKey(contractManageVO.getContractStatus())) {
                    contractManageVO.setContractStatusName(contractStatusMap.get(contractManageVO.getContractStatus()));
                }
                if (contractTypeMap != null && contractTypeMap.containsKey(contractManageVO.getContractType())) {
                    contractManageVO.setContractTypeName(contractTypeMap.get(contractManageVO.getContractType()));
                }
                if (StringUtils.isNotBlank(contractManageVO.getContractFileAddr())) {
                    if (ContractTypeEnum.CONTRACT_TYPE_1.getContractType().equals(contractManageVO.getContractType())) {
                        // 电子合同全路径？普通供水合同拼接路径
                        contractManageVO.setContractFileAddr(ApplicationConfig.getAccessoryUploadUrl() + Constant.FILE_DOWNLOAD + contractManageVO.getContractFileAddr());
                    } else if (ContractTypeEnum.CONTRACT_TYPE_0.getContractType().equals(contractManageVO.getContractType())) {
                        // 电子合同拼接路径
                        contractManageVO.setContractFileAddr(ApplicationConfig.eleContractUrl + contractManageVO.getContractFileAddr());
                    }
                }
                if (StringUtils.isBlank(contractManageVO.getContractStatus())){
                    contractManageVO.setContractStatusName("未签署");
                }
//                if (StringUtils.equalsAny(contractManageVO.getContractType(),ContractTypeEnum.CONTRACT_TYPE_4.getContractType(),ContractTypeEnum.CONTRACT_TYPE_5.getContractType())){
//                    contractManageVO.setUserNo(null);
//                    contractManageVO.setName(null);
//                    contractManageVO.setCertNo(null);
//                }
            }
            MessageBean<PageInfo> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查看合同管理列表成功", PageInfo.class);
            messageBean.setData(new PageInfo<>(contractManageList));
            return messageBean;
        }catch (Exception e){
            logger.error("查询合同列表失败:"+e.getMessage(),e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询合同列表失败: " + e.getMessage(), null);

        }



    }
}
